<?php

/**
 * class Server
 * a little server for github webhook
 *
 * @author handsomeshili <handsomeshili@gmail.com>
 * @version 2016-02-22
 */
class Server {
	private $server;

	// 配置
	public function __construct() {
		$this->server = new swoole_server('0.0.0.0', 9203);
		$this->server->set( array(
				'worker_num'	=> 8,
				'daemonsize'	=> false,
				'max_request'	=> 300,
				'dispatch_mode'	=> 2,
				'debug_mode'	=> 1
			));
		$this->server->on('WorkerStart', array($this, 'onWorkerStart'));
		$this->server->on('Start', array($this, 'onStart'));
		$this->server->on('Connect', array($this, 'onConnect'));
		$this->server->on('Receive', array($this, 'onReceive'));
		$this->server->on('Close', array($this, 'onClose'));

        $this->server->start();
	}

	public function onWorkerStart($server) {
		// codes....
	}

	public function onStart($server) {
		echo 'Server starting...';
	}

	public function onConnect($server, $fd, $from_id) {
		$server->send($fd, 'Hello {$fd}');
	}

	public function onReceive($server, $fd, $from_id, $data) {
		// todo get request uri
        // $server->send($fd, 'received from you client {$fd}, your data is ' . $data . "\n");
        $HttpRequest = $_SERVER['REQUEST_URI'];
        $server->send($fd, 'your request uri is: ' . $HttpRequest . "\n");
	}

	public function onClose($server, $fd, $from_id) {
		echo "Client {$fd} close connection\n";
	}

}


$serv = new Server();
